const WebSocket = require("ws");

const wss = new WebSocket.Server({
    port: 7080
});

/*wss.broadcast=function broadcast(msg) {      //服务端广播消息
    wss.clients.forEach(function each (client) {
        if(client.readyState==WebSocket.OPEN ){
            client.send(msg);
        }
    })
};

wss.on('connection',function connection(ws,req) {

    ws.on('message',function incoming(message) {
        console.log(message);
        wss.broadcast(message);
    });

});*/

wss.on('connection', function connection(ws) {
	
  ws.on('message', function incoming(data) {
    // Broadcast to everyone else.
    wss.clients.forEach(function each(client) {
      if (client !== ws && client.readyState === WebSocket.OPEN) {
        client.send(data);
      }
    });
  });
});

